package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.common.minio.MinIOFileStorageService;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.ThreadLocalUtils;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsService wmNewsService;

    @Override
    public PageResponseResult listChannel(WmChannelDto dto) {

        //参数校验
        dto.checkParam();

        //定义查询条件
        IPage<WmChannel> iPage = new Page<>(dto.getPage(), dto.getSize());
        QueryWrapper<WmChannel> queryWrapper = new QueryWrapper<>();
        //模糊查询
        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like("name", dto.getName());
        }

        //排序
        queryWrapper.orderByDesc("created_time");

        //执行查询
        iPage = page(iPage, queryWrapper);

        //获取结果进行封装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        pageResponseResult.setErrorMessage("查询成功");

        //返回结果
        return pageResponseResult;


    }

    @Override
    public Object updateChannel(WmChannel wmChannel) {
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("channel_id", wmChannel.getId());
        List<WmNews> list = wmNewsService.list(queryWrapper);
        //表示有关联自媒体新闻，不允许禁用
        if (CollectionUtils.isNotEmpty(list) && wmChannel.getStatus().equals(false)) {
            throw new LeadNewsException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        return ResponseResult.okResult(updateById(wmChannel));
    }

    //根据id删除频道
    @Override
    public Object removeById1(Integer id) {
        WmChannel wmChannel = getById(id);
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("channel_id", wmChannel.getId());
        List<WmNews> list = wmNewsService.list(queryWrapper);
        //表示有关联自媒体新闻，不允许删除
        if (CollectionUtils.isNotEmpty(list) ) {
            throw new LeadNewsException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        return ResponseResult.okResult(removeById(id));
    }
}
